package com.luoran.bartalks.action;

import java.util.Date;
import java.util.Map;

import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.bartalks.core.UserContext;
import com.luoran.bartalks.core.ext.Pageable;
import com.luoran.bartalks.entity.BaseInfo;
import com.luoran.bartalks.entity.biz.BtContainerTemplate;
import com.luoran.bartalks.service.IBtCodingRuleService;
import com.luoran.bartalks.service.IBtContainerTemplateService;
import com.luoran.bartalks.utils.EnumBillType;

@Controller
@RequestMapping("/containerTemplate")
public class BtContainerTemplateAction implements Pageable {

	@Autowired
	private IBtContainerTemplateService templateService;

	@Autowired
	private IBtCodingRuleService codingRuleService;

	private static final Logger log = LoggerFactory.getLogger(BtContainerTemplateAction.class);

	/**
	 * 容器模板管理列表页面
	 * 
	 * @return
	 */
	@RequestMapping
	public String queryPosition() {
		log.info("跳转到>>容器模板管理列表页面");
		return "basics/containerTemplate-list";
	}

	// 分页展示
	@Override
	public PageQuery<? extends BaseInfo> getQueryList(PageQuery<? extends BaseInfo> pageQuery) {
		log.info("获取容器模板管理列表数据,page:" + pageQuery.getPageNumber() + "size:" + pageQuery.getPageSize());
		@SuppressWarnings("unchecked")
		Map<String, String> map = (Map<String, String>) pageQuery.getParas();
		map.put("companyId", UserContext.get().getCompanyId());// 添加查询参数（公司ID）
		if (StringUtils.isEmpty(pageQuery.getOrderBy())) {
			pageQuery.setOrderBy("creat_time DESC");
		}
		return templateService.getQueryList(pageQuery);
	}

	/*
	 * 生成容器模板编码
	 */
	@RequestMapping("productContainerCode")
	@ResponseBody
	public JSONObject productCode() {
		return codingRuleService.generateCoding(EnumBillType.CONTAINERTEMPLATE, UserContext.get().getCompanyId());
	}

	@RequestMapping("addContainerTemplate")
	@ResponseBody
	public JSONObject addContainerTemplate(BtContainerTemplate template) {
		log.info("新增容器模板信息>>");
		JSONObject res = new JSONObject();
		res = check(template);
		if (!res.getBooleanValue("state")) {
			return res;
		}
		if (StringUtils.isEmpty(template.getTemplateCode())) {
			res.put("msg", "编码不可以为空！");
			res.put("state", false);
			return res;
		} else if (template.getTemplateCode().length() > 100) {
			res.put("msg", "编码长度限制为100！");
			res.put("state", false);
			return res;
		}

		// 搜索是否已经存在
		BtContainerTemplate persistentTemplate = templateService.getContainerTemplateByCode(template.getTemplateCode(),
				UserContext.get().getCompanyId());
		if (persistentTemplate == null) {
			template.setCreatTime(new Date());
			template.setFounder(UserContext.get().getCompanyId());
			template.setCompanyId(UserContext.get().getCompanyId());
			template = templateService.addContainerTemplate(template);
			res.put("msg", "新增成功！");
			res.put("state", true);
			return res;
		} else {
			res.put("msg", "编码“" + template.getTemplateCode() + "”已被其他引用！");
			res.put("state", true);
			return res;
		}

	}

	private JSONObject check(BtContainerTemplate template) {
		JSONObject res = new JSONObject();
		if (StringUtils.isEmpty(template.getTemplateName())) {
			res.put("msg", "名称不可以为空！");
			res.put("state", false);
			return res;
		} else if (template.getTemplateType() == -1) {
			res.put("msg", "请选择容器类型！");
			res.put("state", false);
			return res;
		}
		try {
			template.getLongs();
		} catch (Exception e) {
			res.put("msg", "长度输入错误，请输入数字！");
			res.put("state", false);
			return res;
		}
		try {
			template.getWide();
		} catch (Exception e) {
			res.put("msg", "宽度输入错误，请输入数字！");
			res.put("state", false);
			return res;
		}
		try {
			template.getHigh();
		} catch (Exception e) {
			res.put("msg", "高度输入错误，请输入数字！");
			res.put("state", false);
			return res;
		}
		try {
			template.getBearWeight();
		} catch (Exception e) {
			res.put("msg", "自重输入错误，请输入数字！");
			res.put("state", false);
			return res;
		}
		try {
			template.getDeadWeight();
		} catch (Exception e) {
			res.put("msg", "承重输入错误，请输入数字！");
			res.put("state", false);
			return res;
		}
		res.put("state", true);
		return res;
	}

	@RequestMapping("editContainerTemplate")
	@ResponseBody
	public JSONObject editContainerTemplate(BtContainerTemplate template) {
		log.info("编辑容器模板信息》》");
		JSONObject res = new JSONObject();
		res = check(template);
		if (!res.getBooleanValue("state")) {
			return res;
		} else {
			templateService.editContainerTemplate(template);
			res.put("msg", "修改成功！");
			res.put("state", true);
			return res;
		}

	}

	@RequestMapping("/deleteContainerTemplate/{id}")
	@ResponseBody
	public JSONObject deleteContainerTemplate(@PathVariable("id") String id) {
		log.info("删除容器模板信息>>");
		JSONObject res = new JSONObject();
		templateService.deleteContainerTemplate(id);
		res.put("msg", "删除成功！");
		res.put("state", true);
		return res;

	}

	@GetMapping("/getContainerTemplate/{id}")
	@ResponseBody
	public JSONObject getContainerTemplate(@PathVariable("id") String id) {
		log.info("根据位置 Id 获取容器模板信息》》");
		JSONObject res = new JSONObject();
		BtContainerTemplate template = templateService.getContainerTemplateById(id);
		res.put("obj", template);
		res.put("state", true);
		return res;
	}
}
